package com.jio.util;

import mapper.AccountMapper;
import com.jio.javabean.Account;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;

public class AccountTest {
    SqlSession session;
    AccountMapper mapper;
    @Before
    public void a() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
         mapper = session.getMapper(AccountMapper.class);

    }

    @After
    public void b() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void insertAccount() {
       Account a =new Account("a","a");
       mapper.insertAccount(a);
    }
    //使用UUID生成主键
    //1)在后台生成主键直接存入数据库
    @Test
    public void insertAccount1() {
        String id = UUID.randomUUID().toString().replace("-", "");
        Account a =new Account(id,"a1");
        mapper.insertAccount1(a);
    }

    @Test
    public void insertAccount2() {
        Account a =new Account("a2");
        mapper.insertAccount2(a);
    }
}